const http = require('http');
const cheerio = require('cheerio');
const db = require('./db');
const events = require('events');


let siteUrl = 'http://www.air-level.com/';
//判断网站AQI是否已经更新,若已经更新则将其爬取到数据库
let currentTime = null;
let flag = false;

let options = {
    hostname: 'www.air-level.com',
    port: 80, //端口号 https默认端口 443， http默认的端口号是80
    path: siteUrl,
    method: 'GET',
    headers: {
        "Connection": "keep-alive",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
    }//伪造请求头
};

/*
**获取全国各个城市AQI数据的url
*/
function getUrl(res) {
    let chunks = [];
    let size = 0;

    res.on('data', function (chunk) {
        chunks.push(chunk);
        size += chunk.length;
    });
    res.on('end', function () {
        let data = Buffer.concat(chunks, size);
        let document = data.toString();

        let $ = cheerio.load(document);
        
        let a = $('#citylist .citynames a');
        const time = $('.text-center h4').text();

        db.getCities({ name: '北京' }, 'time', (data) => {
            currentTime = new Date(time);

            console.log('Cities数据库时间 ' + data[0].time.toString());
            console.log('Cities网页时间 ' + currentTime.toString());

            if (currentTime - data[0].time == 0) {
                console.log('网页未更新');
                flag = false;
            } else {
                flag = true;
                console.log('爬取数据');
                //i = 1 是为了跳过热门城市 以免造成重复
                for (let i = 1; i < a.length; i++) {
                    db.addCities(a[i].children[0].data, siteUrl + a[i].attribs['href'], 0, '', '', currentTime);
                }
            }
        });
    });
}


// http.get(options, getUrl);  

function getAQI(name, url, time) {
    http.get(url, (res) => {
        let chunks = [];
        let size = 0;

        res.on('data', function (chunk) {
            chunks.push(chunk);
            size += chunk.length;
        });

        // console.log(name, url, time);
        res.on('end', () => {
            // console.log('开始爬取cityDetail');
            let data = Buffer.concat(chunks, size);
            let document = data.toString();
            let $ = cheerio.load(document);
            let trs = $('tr').not('.active');

            const aqiLevel = $('span.aqi-bg').text().split(' ');
            if (aqiLevel[0] && aqiLevel[1] && aqiLevel[0] != 0) {
                db.updateCities({ name, time }, aqiLevel[0], aqiLevel[1]);
            }
            for (let i = 0; i * 6 < trs.children().length; i++) {
                db.addCityDetail(name,
                    trs.children().eq(i * 6).text(),
                    trs.children().eq(i * 6 + 1).text().replace('-', ''),
                    trs.children().eq(i * 6 + 2).text(),
                    trs.children().eq(i * 6 + 3).text().replace(' μg/m3', '').replace('-', ''),
                    trs.children().eq(i * 6 + 4).text().replace(' μg/m3', '').replace('-', ''),
                    trs.children().eq(i * 6 + 5).text(),
                    '',
                    time);
            }
        });
    });
}


// db.getCities({}, 'name url time', async (data) => {
//     // console.log(data);
//     for (let i = 0; i < data.length; i++) {
//         await getAQI(data[i].name, data[i].url, data[i].time);
//     }
// }); 

async function intervalFunAsync () {
    let promise = new Promise((resolve, reject) => {
        http.get(options, getUrl);
    })
    await pormise;
    intervalFunc2();
}

function intervalFunc1() {
    http.get(options, getUrl);
}

function intervalFunc2() {
    if (flag) {
        console.log('开始爬取cityDetail');

        db.getCities({ time: currentTime }, 'name url time', (data) => {
            for (let i = 0; i < data.length; i++) {
                getAQI(data[i].name, data[i].url, data[i].time);
            }
            // let i = 0;
            // setTimeout(() => {
            //     if (data.length) {
            //         getAQI(data[i].name, data[i].url, data[i].time);
            //         i++;
            //     } else {
            //         console.log('CityDetail爬取完成');
            //     }
            // }, 200);
        });
    }
}

// 一次爬取
// setTimeout(function funcInside() {
//     http.get(options, getUrl);
//     // setTimeout(funcInside, 1000);
// }, 1000);

// setTimeout(() => {
//     if (flag) {
//         console.log('开始爬取cityDetail');

//         db.getCities({ time: currentTime }, 'name url time', (data) => {
//             // console.log(data);
//             for (let i = 0; i < data.length; i++) {
//                 getAQI(data[i].name, data[i].url, data[i].time);
//             }
//         });
//     }
// }, 3000);

intervalFunc1();

setTimeout(intervalFunc2, 1000 * 30)

//3600000ms
setInterval(intervalFunc1, 1000 * 60 * 60);

// intervalFunc1();

setInterval(intervalFunc2, 1000 * 60 * 60 + 1000 *  30);

// getAQI(urls[0]);
// db.getCityDetail();

